# -------------------------------------------------------------------
# Purpose: Creates Table B50
# Author:  Max Posch, 25/07/2025
# Usage:   Source this script to generate the table.
# -------------------------------------------------------------------
# Check that required paths exist
stopifnot(dir.exists(pdataconfanalysis))
stopifnot(dir.exists(poutputappendix))


# Load data
load(file.path(pdataconfanalysis, "patentLevel19001940.RData"))


# Regressions
o <- list()
o <- append(o, list(feols(break_p80_rrfsim05 ~ entropy_patent_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
o <- append(o, list(feols(break_p80_rrfsim05 ~ entropy_patent_coo_adjp_ws  | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
o <- append(o, list(feols(break_p80_rrfsim05 ~ entropy_patent_coo_adjp_ws | no_of_inventors + gisjoin_1900^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
o <- append(o, list(feols(n_uspto_techn ~ entropy_patent_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
o <- append(o, list(feols(n_uspto_techn ~ entropy_patent_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
o <- append(o, list(feols(n_uspto_techn ~ entropy_patent_coo_adjp_ws | no_of_inventors + gisjoin_1900^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))

p <- list()
p <- append(p, list(feols(break_p80_rrfsim05 ~ entropy_patent_namelast_mp_adjp_ws + entropy_patent_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year, d, weight = d$wgt)))
p <- append(p, list(feols(break_p80_rrfsim05 ~ entropy_patent_namelast_mp_adjp_ws + entropy_patent_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, d, weight = d$wgt)))
p <- append(p, list(feols(break_p80_rrfsim05 ~ entropy_patent_namelast_mp_adjp_ws + entropy_patent_coo_adjp_ws | no_of_inventors + gisjoin_1900^year + tech_category^year, d, weight = d$wgt)))
p <- append(p, list(feols(n_uspto_techn ~ entropy_patent_namelast_mp_adjp_ws + entropy_patent_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year, d, weight = d$wgt)))
p <- append(p, list(feols(n_uspto_techn ~ entropy_patent_namelast_mp_adjp_ws + entropy_patent_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, d, weight = d$wgt)))
p <- append(p, list(feols(n_uspto_techn ~ entropy_patent_namelast_mp_adjp_ws + entropy_patent_coo_adjp_ws | no_of_inventors + gisjoin_1900^year + tech_category^year, d, weight = d$wgt)))

r <- list()
r <- append(r, list(feols(break_p80_rrfsim05 ~ entropy_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
r <- append(r, list(feols(break_p80_rrfsim05 ~ entropy_coo_adjp_ws  | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
r <- append(r, list(feols(break_p80_rrfsim05 ~ entropy_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
r <- append(r, list(feols(n_uspto_techn ~ entropy_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
r <- append(r, list(feols(n_uspto_techn ~ entropy_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))
r <- append(r, list(feols(n_uspto_techn ~ entropy_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, patentLevel19001940, weight = patentLevel19001940$wgt)))

i <- list()
i <- append(i, list(feols(entropy_patent_coo_adjp_ws ~ entropy_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year, d, weight = d$wgt)))
i <- append(i, list(feols(entropy_patent_coo_adjp_ws ~ entropy_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, d, weight = d$wgt)))
i <- append(i, list(feols(entropy_patent_coo_adjp_ws ~ entropy_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, d, weight = d$wgt)))
i <- append(i, list(feols(entropy_patent_namelast_mp_adjp_ws ~ entropy_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year, d, weight = d$wgt)))
i <- append(i, list(feols(entropy_patent_namelast_mp_adjp_ws ~ entropy_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, d, weight = d$wgt)))
i <- append(i, list(feols(entropy_patent_namelast_mp_adjp_ws ~ entropy_coo_adjp_ws | no_of_inventors + gisjoin_1900 + statefip^year + tech_category^year, d, weight = d$wgt)))


# Create table
y1_mean <- round(weighted.mean(patentLevel19001940$break_p80_rrfsim05, patentLevel19001940$wgt), 2)
y1_sd <- round(sqrt(weighted.mean((patentLevel19001940$break_p80_rrfsim05 - y1_mean)^2, patentLevel19001940$wgt)), 2)
y2_mean <- round(weighted.mean(patentLevel19001940$n_uspto_techn, patentLevel19001940$wgt), 2)
y2_sd <- round(sqrt(weighted.mean((patentLevel19001940$n_uspto_techn - y2_mean)^2, patentLevel19001940$wgt)), 2)
y1 <- paste0("\\makecell{Breakthrough patent \\\\ indicator \\\\ (mean = ", y1_mean, ", sd = ", y1_sd, ")}")
y2 <- paste0("\\makecell{Technologies \\\\ per patent \\\\ (mean = ", y2_mean, ", sd = ", y2_sd, ")}")

setFixest_dict(
  c(
    entropy_patent_namelast_mp_adjp_ws = "Surname diversity of patent",
    entropy_patent_coo_adjp_ws = "Ancestral-country diversity of patent",
    entropy_coo_adjp_ws = "Ancestral-country diversity",
    break_p80_rrfsim05 = y1, n_uspto_techn = y2,
    year = "Period", statefip = "State", namelast_mp = "Surname", gisjoin_1900 = "County", main_patentclass = "Patent technology class", tech_category = "Patent technology field", no_of_inventors = "Team size"
  )
)

tablename <- file.path(poutputappendix, "tableB50.tex")
etable(o,
  cluster = ~gisjoin_1900,
  fitstat = ~n,
  digits = "r3", digits.stats = "r3",
  file = tablename, replace = TRUE,
  style.tex = style.tex("aer"), tex = TRUE
)
add_table_row(tablename, "\\midrule", "\\multicolumn{2}{l}{\\textit{Panel A}} &  \\multicolumn{5}{c}{}\\\\ \\cmidrule(lr){1-7}")
add_table_row(tablename, "mean =", "\\cmidrule(lr){2-4}  \\cmidrule(lr){5-7}")
move_table_row(tablename, "Observations", "bottomrule")
add_table_row(tablename, "    \\\\", c("\\multicolumn{2}{l}{\\textit{Panel B}} &  \\multicolumn{5}{c}{}\\\\", "\\\\", "\\multicolumn{2}{l}{\\textit{Panel C}} &  \\multicolumn{5}{c}{}\\\\", "\\\\", "\\multicolumn{2}{l}{\\textit{Panel D}} &  \\multicolumn{5}{c}{}\\\\"))

temptable <- file.path(poutputappendix, "temp.tex")
etable(p,
  cluster = ~gisjoin_1900,
  fitstat = ~n,
  order = c("country"),
  digits = "r3", digits.stats = "r3",
  file = temptable, replace = TRUE,
  style.tex = style.tex("aer"), tex = TRUE
)
estimates_rows <- get_estimates_rows(temptable)
add_table_row(tablename, "Panel B", c("\\cmidrule(lr){1-7}", estimates_rows))

temptable <- file.path(poutputappendix, "temp.tex")
etable(r,
  cluster = ~gisjoin_1900,
  fitstat = ~n,
  order = c("country"),
  digits = "r3", digits.stats = "r3",
  file = temptable, replace = TRUE,
  style.tex = style.tex("aer"), tex = TRUE
)
estimates_rows <- get_estimates_rows(temptable)
add_table_row(tablename, "Panel C", c("\\cmidrule(lr){1-7}", estimates_rows))

temptable <- file.path(poutputappendix, "temp.tex")
etable(i,
  cluster = ~gisjoin_1900,
  fitstat = ~n,
  digits = "r3", digits.stats = "r3",
  file = temptable, replace = TRUE,
  style.tex = style.tex("aer"), tex = TRUE
)
estimates_rows <- get_estimates_rows(temptable)
estimates_rows <- collapse_stage1(estimates_rows, c(2, 3, NA, 5, 6, NA))
add_table_row(tablename, "Panel D", estimates_rows)
add_table_row(tablename, "Panel D", c("\\cmidrule(lr){1-7}", "& \\multicolumn{2}{c}{\\makecell{Ancestral-country \\\\ diversity of patent}} & & \\multicolumn{2}{c}{\\makecell{Surname \\\\ diversity of patent}} & \\\\", "\\cmidrule(lr){2-3} \\cmidrule(lr){5-6}"))
file.remove(temptable)

cat("Table B50 saved to:", tablename, "\n")